package com.grade.system.controller;

import com.grade.system.entity.Student;
import com.grade.system.mapper.StudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
public class StudentRestController {

    @Autowired
    private StudentMapper studentMapper;

    @RequestMapping("/aj_sel_student_by_all")
    public List<Student> selStudentByAll() {
        return studentMapper.selStudentByAll();
    }

    @RequestMapping("/aj_sel_student_by_id")
    public Student selStudentById(@RequestParam Map<String, Object> map) {
        int id = Integer.parseInt(String.valueOf(map.get("id")));
        return studentMapper.selStudentById(id);
    }

    @RequestMapping("/aj_del_student_by_id")
    @ResponseBody
    public int delStudentById(@RequestParam Map<String, Object> map) {
        int id = Integer.parseInt(String.valueOf(map.get("id")));
        return studentMapper.delStudentById(id) ? NetState.STATE_OK.value : NetState.STATE_ERROR.value;
    }


    @RequestMapping("/aj_ins_student")
    @ResponseBody
    public int insStudent(@RequestParam Map<String, Object> map) {
        System.out.println(map);
        String name = String.valueOf(map.get("name"));
        int sex = Integer.parseInt(String.valueOf(map.get("sex")));
        int age = Integer.parseInt(String.valueOf(map.get("age")));
        String phone = String.valueOf(map.get("phone"));
        String native_place = String.valueOf(map.get("native_place"));
        Student student = new Student(name, sex, age, phone, native_place);
        return studentMapper.insStudent(student) ? NetState.STATE_OK.value : NetState.STATE_ERROR.value;
    }

    @RequestMapping("/aj_upd_student_by_id")
    @ResponseBody
    public int updStudentById(@RequestParam Map<String, Object> map) {
        System.out.println(map);
        int id = Integer.parseInt(String.valueOf(map.get("id")));
        String name = String.valueOf(map.get("name"));
        int sex = Integer.parseInt(String.valueOf(map.get("sex")));
        int age = Integer.parseInt(String.valueOf(map.get("age")));
        String phone = String.valueOf(map.get("phone"));
        String native_place = String.valueOf(map.get("native_place"));
        Student student = new Student(id, name, sex, age, phone, native_place);
        return studentMapper.updStudentById(student) ? NetState.STATE_OK.value : NetState.STATE_ERROR.value;
    }

}
